<template>
  <div class="com-page">
    <div
        style="margin: 200px auto; background-color: #fff; width: 350px; height: 300px; padding: 20px; border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 1.2rem">
        <el-select v-model="identify" size="medium" style="width: 100px;margin-right: 10px">
          <el-option label="管理员" value="admain"></el-option>
          <el-option label="学生" value="student"></el-option>
        </el-select>
        <b>登 录</b>
      </div>
      <el-form :model="user" :rules="rules" ref="userForm">
        <el-form-item prop="username">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password
                    v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: right">
          <el-button type="primary" size="small" autocomplete="off" @click="login"
                     :disabled="identify=='admain'|| identify=='student'? false:true">登录</el-button>
          <el-button type="warning" size="small" autocomplete="off" @click="$router.push('/register')"
                     :disabled="identify=='admain'? false:true">注册
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      user: {},
      identify: '',
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    login() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {  // 表单校验合法
          this.request.post("/" + this.identify + "/login", this.user).then(res => {
            if (this.identify == 'admain') {
              if (res.code == 20040) {
                this.$message.error(res.msg)
              } else if (res.code == 20041) {
                this.$router.push("/manage/stuInfos")
                this.$message.success(res.msg)
                localStorage.setItem("user", JSON.stringify(res.data))
              } else {
                this.$message.error(res.msg)
              }
            } else if (this.identify == 'student') {
              if (res.code == 20040) {
                this.$message.error(res.msg)
              } else if (res.code == 20041) {
                this.$router.push("/stuSignIn/basicInfo")
                this.$message.success(res.msg)
                // 将查询出的基础学生基础信息储存到studentInformation中以用作基础信息展示
                localStorage.setItem("user", JSON.stringify(res.data))
              } else {
                this.$message.error(res.msg)
              }
            }

          })
        } else {
          return false;
        }
      });
    }
  }
}
</script>
<style scoped>
.com-page {
  background-image: linear-gradient(to bottom right, #1e5670, #bbabd0);
}
</style>